package com.softer.chengxin.modify.dao;

import com.softer.chengxin.modify.entity.BaseModifyDetail;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @author Gong Zhiyang
 * @date 2019/9/10
 */
@Mapper
@Repository
public interface BaseModifyDetailDao {

    /**
     * 插入变更数据
     *
     * @param baseModifyDetail 变更数据
     * @return 处理结果
     */
    @Insert("INSERT INTO company_modify_detail(modify_id,param,value,gmt_create,gmt_modify) VALUES(#{b.modifyId},#{b.param},#{b.value},now(),now())")
    int insert(@Param("b") BaseModifyDetail baseModifyDetail);

    /**
     * 查询本次变更中，是否存在了该字段
     *
     * @param modifyId 变更ID
     * @param param    变更参数
     * @return 变更参数
     */
    @Select("select param from company_modify_detail where modify_id=#{id} and param=#{param}")
    String getParam(@Param("id") int modifyId, @Param("param") String param);

    /**
     * 根据业务ID查询最近一次的变更信息
     *
     * @param id 业务ID
     * @param type 变更类型（1企业基本信息变更，0项目基本信息变更）
     * @return 变更信息
     */
    @Select("SELECT param,value FROM company_modify_detail WHERE modify_id=(SELECT id FROM company_modify WHERE business_id=#{id} and type=#{type} and state = 0 ORDER BY gmt_create DESC LIMIT 1)")
    List<Map<String, Object>> getDetailListByBusinessId(@Param("id") int id, @Param("type") String type);
}
